import { EventKey } from "../common/Key";

export const UIMgr = {
    cacheUIList: [],// 缓存的ui
    uiList: [],    // 显示的ui
    rootUI: null,

    setRoot(rootUI) {
        this.rootUI = rootUI;
    },

    clear() {
        this.cacheUIList = [];
        this.uiList = [];
    },

    isAlive(uiName) {
        this.uiList = this.uiList || [];
        let isAlive = false;
        for (var i = this.uiList.length - 1; i >= 0; i--) {
            var temp = this.uiList[i];
            if (uiName == temp.name) {
                isAlive = true;
            }
        }
        return isAlive;
    },

    openUI(uiName, param, action, zIndex) {
        G.onFire.fire(EventKey.EVENT_BLOCK_INPUT);
        this.cacheUIList = this.cacheUIList || [];
        this.uiList = this.uiList || [];
        // 缓存--
        for (var i = 0; i < this.cacheUIList.length; i++) {
            var temp = this.cacheUIList[i];
            if (temp && temp.name === uiName) {
                temp.active = true;
                let node = this.rootUI ? this.rootUI : cc.Canvas.instance.node;
                temp.parent = node;
                temp.zIndex = zIndex || 0;
                this.uiList.push(temp)
                this.cacheUIList.splice(i, 1);

                var panel = temp.getComponent(uiName);
                if (panel) {
                    panel.onEnter(param,action);
                }
                G.onFire.fire(EventKey.EVENT_CANCEL_BLOCK_INPUT);
                return;
            }
        }
        // 非缓存--
        cc.loader.loadRes('prefabs/panels/' + uiName, cc.Prefab, (err, prefab) => {
            var temp = cc.instantiate(prefab);
            if (!temp) {
                return;
            }
            let node = this.rootUI ? this.rootUI : cc.Canvas.instance.node;
            temp.parent = node;
            temp.zIndex = zIndex || 0;
            this.uiList.push(temp)
            var panel = temp.getComponent(uiName);
            if (panel) {
                panel.onEnter(param,action);
            }
            G.onFire.fire(EventKey.EVENT_CANCEL_BLOCK_INPUT);
        });
    },


    // 可以传入字符串 或者cc.Node
    closeUI(target,args) {
        for (var i = this.uiList.length - 1; i >= 0; i--) {
            var temp = this.uiList[i];
            if (typeof target == 'string') {
                if (temp && temp.name === target) {
                    temp.active = false;
                    temp.removeFromParent(true);
                    this.cacheUIList.push(temp);
                    this.uiList.splice(i, 1);

                    var panel = temp.getComponent(temp.name);
                    if (panel) {
                        panel.onExit(args);
                    }
                }

            } else {
                if (target == temp) {
                    temp.active = false;
                    temp.removeFromParent(true);
                    this.cacheUIList.push(temp);
                    this.uiList.splice(i, 1);

                    var panel = temp.getComponent(temp.name);
                    if (panel) {
                        panel.onExit(args);
                    }
                }
            }
        }
    },

    closeAllUI() {
        for (var i = this.uiList.length - 1; i >= 0; i--) {
            var temp = this.uiList[i];
            temp.active = false;
            temp.removeFromParent(true);
            this.cacheUIList.push(temp);
            this.uiList.splice(i, 1);

            var panel = temp.getComponent(temp.name);
            if (panel) {
                panel.onExit();
            }
        }
    },

    // 寻找从节点移除的UI
    findCacheUI(uiName) {
        for (var i = this.cacheUIList.length - 1; i >= 0; i--) {
            var temp = this.cacheUIList[i];
            if (temp && temp.name === uiName) {
                return temp;
            }
        }
    },

    // 寻找添加到节点上的UI
    findUI(uiName) {
        for (var i = this.uiList.length - 1; i >= 0; i--) {
            var temp = this.uiList[i];
            if (temp && temp.name === uiName) {
                return temp;
            }
        }
    },

    destroyUI(target) {
        var temp;

        for (var i = this.cacheUIList.length - 1; i >= 0; i--) {
            if (typeof target == 'string') {
                if (this.cacheUIList[i] && this.cacheUIList[i].name === target) {
                    temp = this.cacheUIList.splice(i, 1)[0];
                    // break;
                }
            } else {
                if (this.cacheUIList[i] && this.cacheUIList[i] === target) {
                    temp = this.cacheUIList.splice(i, 1)[0];
                    // break;
                }
            }
        }
        if (!temp) {
            for (var i = this.uiList.length - 1; i >= 0; i--) {
                if (typeof target == 'string') {
                    if (this.uiList[i] && this.uiList[i].name === target) {
                        temp = this.uiList.splice(i, 1)[0];
                        // break;
                    }
                } else {
                    if (this.uiList[i] && this.uiList[i] === target) {
                        temp = this.uiList.splice(i, 1)[0];
                        // break;
                    }
                }
            }
        }

        if (temp) {
            var panel = temp.getComponent(temp.name);
            if (panel) {
                panel.onExit();
            }
            temp.destroy();
        } else {
            cc.log('不存在该节点');
        }
    }
}
